package com.example.accessibilitydemo.service;

import java.util.List;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.Build;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;

import com.example.accessibilitydemo.util.PreferenceUtil;

public class WeChatAddNearService extends BaseAccessibilityService {

	private static final String TAG = WeChatAddNearService.class.getSimpleName();

	private String hello = "您好";
	private int num = 10;
	private boolean addnear = false;

	int count = 0;// 记录已打招呼的人数
	int page = 1;// 记录附近的人列表页码,初始页码为1
	int prepos = -1;// 记录页面跳转来源，0--从附近的人页面跳转到详细资料页，1--从打招呼页面跳转到详细资料页

	@Override
	public void onAccessibilityEvent(AccessibilityEvent event) {
		super.onAccessibilityEvent(event);

		addnear = PreferenceUtil.getBoolean("addnear", false);
		num = Integer.parseInt(PreferenceUtil.getString("say_hi_num", "10"));
		hello = PreferenceUtil.getString("say_hi_txt", "您好");

		Log.i("name", "addnear: " + addnear + ",num:" + num + ",hello:" + hello);

		if (count >= num) {// 完成任务退出
			PreferenceUtil.commitBoolean("addnear", false);
			addnear = false;
			count = 0;
			performHomeClick();
		}

		if (addnear == false)
			return;

		int eventType = event.getEventType();
		if (eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
				&& event.getClassName().equals("com.tencent.mm.ui.LauncherUI")) {
			// 记录打招呼人数置零
			count = 0;
			// 当前在微信聊天页就点开发现
			delayClickViewByText("发现", 2000);
			// 然后跳转到附近的人
			delayClickViewByText("附近的人", 2000);

		} else if (event.getClassName().equals("com.tencent.mm.plugin.nearby.ui.NearbyFriendsUI")
				&& eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
			prepos = 0;
			// 当前在附近的人界面就点选人打招呼
			AccessibilityNodeInfo nodeInfo = getRootInActiveWindow();
			List<AccessibilityNodeInfo> list = nodeInfo.findAccessibilityNodeInfosByText("米以内");
			Log.i("name", "已经给附近的人打招呼的数量: " + count);
			if (count < (list.size() * page)) {
				list.get(count % list.size()).performAction(AccessibilityNodeInfo.ACTION_CLICK);
				list.get(count % list.size()).getParent().performAction(AccessibilityNodeInfo.ACTION_CLICK);
			} else if (count == list.size() * page) {
				// 本页已全部打招呼，所以下滑列表加载下一页，每次下滑的距离是一屏
				for (int i = 0; i < nodeInfo.getChild(0).getChildCount(); i++) {
					if (nodeInfo.getChild(0).getChild(i).getClassName().equals("android.widget.ListView")) {
						AccessibilityNodeInfo node_lsv = nodeInfo.getChild(0).getChild(i);
						node_lsv.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
						page++;
						new Thread(new Runnable() {
							@Override
							public void run() {
								try {
									Thread.sleep(1000);
								} catch (InterruptedException mE) {
									mE.printStackTrace();
								}

								List<AccessibilityNodeInfo> list_ = findAllViewByText("米以内");
								// 滑动之后，上一页的最后一个item为当前的第一个item，所以从第二个开始打招呼
								list_.get(1).performAction(AccessibilityNodeInfo.ACTION_CLICK);
								list_.get(1).getParent().performAction(AccessibilityNodeInfo.ACTION_CLICK);
							}
						}).start();
					}
				}
			}
		} else if (event.getClassName().equals("com.tencent.mm.plugin.profile.ui.ContactInfoUI")
				&& eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
			if (prepos == 1) {
				// 从打招呼界面跳转来的，则点击返回到附近的人页面
				performBackClick();
				count++;
			} else if (prepos == 0) {
				// 从附近的人跳转来的，则点击打招呼按钮
				List<AccessibilityNodeInfo> list = findAllViewByText("打招呼");
				if (list.size() > 0) {
					list.get(list.size() - 1).performAction(AccessibilityNodeInfo.ACTION_CLICK);
					list.get(list.size() - 1).getParent().performAction(AccessibilityNodeInfo.ACTION_CLICK);
				} else {
					// 如果遇到已加为好友的则界面的“打招呼”变为“发消息"，所以直接返回上一个界面并记录打招呼人数+1
					performBackClick();
					count++;
				}
			}
		} else if (event.getClassName().equals("com.tencent.mm.ui.contact.SayHiEditUI")
				&& eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
			// 当前在打招呼页面
			prepos = 1;
			// 输入打招呼的内容并发送
			inputHello(hello);

			delayClickViewByText("发送", 1000);
		}

	}

	// 自动输入打招呼内容
	private void inputHello(String hello) {
		AccessibilityNodeInfo nodeInfo = getRootInActiveWindow();
		// 找到当前获取焦点的view
		AccessibilityNodeInfo target = nodeInfo.findFocus(AccessibilityNodeInfo.FOCUS_INPUT);
		if (target == null) {
			Log.d(TAG, "inputHello: null");
			return;
		}
		Log.d(TAG, "inputHello: " + hello);
		ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
		ClipData clip = ClipData.newPlainText("sayhi", hello);
		clipboard.setPrimaryClip(clip);
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
			target.performAction(AccessibilityNodeInfo.ACTION_PASTE);
		}
	}
}
